//
//  ThirdViewController.swift
//  SwiftTest
//
//  Created by 齐凯 on 2019/2/15.
//  Copyright © 2019年 QiKai. All rights reserved.
//

import UIKit



class ThirdViewController: UIViewController{
    
    struct Key {
        struct Identity {//cell相关的注册
            static let headerIdentity = "header"
            static let footerIdentity = "footer"
            static let cellIdentity   = "cell"
        }
        struct cellItemSize {//每组cell items的size.width
            static var cellFirstWidth     = KscreenWidth
            static let cellSecondWidth    = (KscreenWidth - 5*2 - 3*4)/5
            static let cellThirdWidth     = (KscreenWidth - 5*2 - 3*6)/7
        }
    }

    
    var collecV:UICollectionView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "三"
//        let layout = TestUICollectionViewFlowLayout.init()
        let layout = UICollectionViewFlowLayout.init()
        //        layout.itemSize = CGSize(width: 60, height: 60)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 2
        layout.scrollDirection = .vertical
        layout.sectionInset = UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5)
        layout.headerReferenceSize = CGSize(width: KscreenWidth, height: 80)
        layout.footerReferenceSize = CGSize(width: KscreenWidth, height: 80)
        
        self.collecV = UICollectionView.init(frame: CGRect.null, collectionViewLayout: layout)
        self.collecV.backgroundColor = UIColor.white
        //注册
        self.collecV.register(CollectionViewCellTest.self, forCellWithReuseIdentifier: Key.Identity.cellIdentity)
        collecV.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Key.Identity.headerIdentity);
        collecV.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: Key.Identity.footerIdentity)
        self.collecV.delegate = self
        self.collecV.dataSource = self
        self.view.addSubview(self.collecV)
        self.collecV.snp.makeConstraints { (make) in
            make.left.right.top.bottom.equalToSuperview()
        }
        
    }
    
}

extension ThirdViewController:UICollectionViewDelegateFlowLayout{
    //MARK: - UICollectionViewDelegateFlowLayout
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        //每组item大小
        if indexPath.section == 0 {
            return CGSize(width: Key.cellItemSize.cellFirstWidth, height: 100)
        }
        if indexPath.section == 1 {
            return CGSize(width: Key.cellItemSize.cellSecondWidth, height: 50)
        }
        
        if indexPath.section == 2 {
            return CGSize(width:Key.cellItemSize.cellThirdWidth, height: 30)
        }
        return CGSize(width: 0, height: 0)
    }
    //
    //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    //
    //        //行间距
    //    }
    //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    //
    //        //item之间的距离
    //    }
    //
    //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    //
    //        //每组之间的距离
    //    }
    //
    //
    //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
    //        //头试图 大小
    //    }
    //    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
    //        //尾部试图大小
    //    }
    
}
extension ThirdViewController:UICollectionViewDelegate,UICollectionViewDataSource{
    //MARK: - UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 0 {
            return 10
        }
        if section == 1 {
            return 13
        }
        if section == 2 {
            return 8
        }
        return 0
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:CollectionViewCellTest = collectionView.dequeueReusableCell(withReuseIdentifier: Key.Identity.cellIdentity, for: indexPath) as! CollectionViewCellTest
        cell.backgroundColor = UIColor.brown
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        if kind == UICollectionView.elementKindSectionHeader{
            let headerView : UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: Key.Identity.headerIdentity, for: indexPath)
            headerView.backgroundColor = UIColor.yellow
            return headerView
        }else{
            let footView : UICollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: Key.Identity.footerIdentity, for: indexPath)
            footView.backgroundColor = UIColor.purple
            return footView
        }
    }
   
    
}
//class TestUICollectionViewFlowLayout: UICollectionViewLayout {
//
//
//
//    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
//    override init() {
//        super.init()
//
//        //        let wideth = (KscreenWidth-7*2 - 5*2)/3
//        //        self.itemSize = CGSize(width: wideth, height: wideth)
//        //        self.minimumLineSpacing = 5
//        //        self.minimumInteritemSpacing = 2
//        //        self.scrollDirection = .vertical
//        //        self.sectionInset = UIEdgeInsets.init(top: 5, left: 5, bottom: 5, right: 5)
//        //        self.headerReferenceSize = CGSize(width: KscreenWidth, height: 80)
//        //        self.footerReferenceSize = CGSize(width: KscreenWidth, height: 80)
//    }
//
//    override func prepare() {
//        //计算布局
////        let a = 1
//        super.prepare()
//        if let sectionCount = self.collectionView?.numberOfSections{
//            for section in 0..<sectionCount{
//                for row in 0..<self.collectionView!.numberOfItems(inSection: section){
//                    let indexPath = IndexPath(row: row, section: section)
//                    let attr = layoutAttributesForItem(at: indexPath)
//                        cellAttribites[indexPath] = attr
//                }
//            }
//        }
////        self.collectionViewContentSize
//    }
//    var cellAttribites:[IndexPath:UICollectionViewLayoutAttributes] = [:]
//
//    override var collectionViewContentSize: CGSize{
//        get{
//            let maxY:CGFloat = cellAttribites.values.max(by: { (attr1, attr2) -> Bool in
//                attr1.frame.maxY > attr2.frame.maxY
//            })?.frame.maxY ?? 0
//            return CGSize(width: UIScreen.main.bounds.width, height: maxY)
//        }
//    }
//    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
////        if cellAttribites.contains(where: { $0.key == indexPath}){
////            return cellAttribites[indexPath]
////        }else{
////            let newAtrribites = UICollectionViewLayoutAttributes(forCellWith: indexPath)
////            cellAttribites[indexPath] = newAtrribites
////            if indexPath.section == 0{
////                newAtrribites.frame = CGRect(x: (50 + 10)*indexPath.row, y: 0, width: 50, height: 50)
////                //                let screenWidth = UIScreen.main.bounds.width
////
////            }
////            return newAtrribites
////        }
//        return Array(cellAttribites.values)
//    }
//    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
//        if cellAttribites.contains(where: { $0.key == indexPath}){
//            return cellAttribites[indexPath]
//        }else{
//            let newAtrribites = UICollectionViewLayoutAttributes(forCellWith: indexPath)
//            cellAttribites[indexPath] = newAtrribites
////            if indexPath.section == 0{
//                newAtrribites.frame = CGRect(x: (50 + 10)*indexPath.row, y: indexPath.section*(100), width: 50, height: 50)
////                let screenWidth = UIScreen.main.bounds.width
//
////            }
//            return newAtrribites
//        }
//    }
//
//}












